package com.kolakcc.loljclient.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.SwingWorker;

import com.gvaneyck.rtmp.TypedObject;
import com.kolakcc.loljclient.StartupClass;
import com.kolakcc.loljclient.model.Champion;
import com.kolakcc.loljclient.model.IconSize;
import com.kolakcc.loljclient.model.swing.FilterComboBoxModel;
import com.kolakcc.loljclient.model.swing.IconSizeComboBoxModel;
import com.kolakcc.loljclient.view.ChampionsView;
import com.kolakcc.loljclient.view.ui.ChampionBox;

public class ChampionsController extends KolaController implements
		ActionListener {
	protected ChampionsView view;
	protected SwingWorker<Void, Champion> championListWorker;
	protected FilterComboBoxModel filterModel;
	protected IconSizeComboBoxModel sizeModel;
	protected ArrayList<Champion> champions;
	protected MouseListener optionalMouseListener;
	String filterString;

	public ChampionsController() {
		this.initializeWorkers();
		this.view = new ChampionsView();
		this.setView(this.view);
		filterString = "All";
		this.champions = new ArrayList<Champion>();
		this.championListWorker.execute();
		this.filterModel = new FilterComboBoxModel();
		this.view.filterComboBox.setModel(this.filterModel);
		this.view.filterComboBox.addActionListener(this);
		this.sizeModel = new IconSizeComboBoxModel();
		this.view.sizeComboBox.setModel(this.sizeModel);
		this.view.sizeComboBox.addActionListener(this);
		this.view.setVisible(true);
	}
	public ChampionsController(MouseListener listener) {
		optionalMouseListener = listener;
		this.view = new ChampionsView();
		filterString = "Playable";
		this.champions = new ArrayList<Champion>();
		this.initializeWorkers();
		this.championListWorker.execute();
		this.filterModel = new FilterComboBoxModel();
		this.filterModel.setSelectedItem("Playable");
		this.sizeModel = new IconSizeComboBoxModel();
		this.sizeModel.setSelectedItem(IconSizeComboBoxModel.MEDIUM);
	}
	public boolean addThisChampion(Champion c) {
		if (this.filterModel.getSelectedItem() == "All") return true;
		else if ((this.filterModel.getSelectedItem() == "Bought") && (c.isOwned())) return true;
		else if ((this.filterModel.getSelectedItem() == "Not bought") && (!c.isOwned())) return true; 
		else if ((this.filterModel.getSelectedItem() == "Playable") && (c.isOwned() || c.isFreeToPlay())) return true;
		else if ((this.filterModel.getSelectedItem() == "Free this week") && (c.isFreeToPlay())) return true;
		return false;
	}
	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == this.view.filterComboBox) {
			IconSize is = (IconSize) this.sizeModel.getSelectedItem();
			this.view.championsPanel.removeAll();
			for (Champion c : this.champions) {
				try {
					if (addThisChampion(c)) {
						ChampionBox cb = new ChampionBox(c);
						if (optionalMouseListener != null) cb.addMouseListener(optionalMouseListener);
						cb.setSize(is.number, is.number);
						this.view.championsPanel.add(cb);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			this.view.championsPanel.repaint();
		} else if (event.getSource() == this.view.sizeComboBox) {
			IconSize is = (IconSize) this.sizeModel.getSelectedItem();
			this.view.setIconSizes(is.number);
		}
	}

	public void initializeWorkers() {
		this.championListWorker = new SwingWorker<Void, Champion>() {
			public Void doInBackground() throws Exception {
				int id = StartupClass.Client.invoke("inventoryService",
						"getAvailableChampions", new Object[] {});
				TypedObject championData = StartupClass.Client.getResult(id)
						.getTO("data");
				Object[] list = championData.getArray("body");
				for (Object champion : list) {
					TypedObject castedChampion = (TypedObject) champion;
					Champion fromDatabase = Champion
							.getChampionFromID(castedChampion
									.getInt("championId"));
					fromDatabase.setOwned(castedChampion.getBool("owned")); // TODO: model all champion fields
					fromDatabase.setFreeToPlay(castedChampion
							.getBool("freeToPlay"));
					this.publish(fromDatabase);
				}
				return null;
			}

			public void done() {
				ChampionsController.this.initializeWorkers();
				ActionEvent e = new ActionEvent(ChampionsController.this.view.filterComboBox, ActionEvent.ACTION_PERFORMED, "filter");
				ChampionsController.this.actionPerformed(e);
				ChampionsController.this.view.championsPanel.revalidate();
			}

			public void process(List<Champion> progress) {
				ChampionsController.this.champions.addAll(progress);
			}
		};
	}
}
